package com.hopu.oa.controller.process;

import com.hopu.oa.dao.user.UserDao;
import com.hopu.oa.entity.process.AoaProcessList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.impl.process.BursementServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class myApplicationController {
    @Autowired
    private UserDao userDao;

    @Autowired
    private BursementServiceImpl bursementService;

    @RequestMapping("/flowmanage")
    public String selectAll(String baseKey, Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "8") int size, HttpSession session) {
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        //总数据
        int totalElements = bursementService.selectAll(userId, baseKey, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaProcessList> list = bursementService.selectAll(userId, baseKey, (page - 1) * size, size);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);

        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("prolist", list);
        model.addAttribute("url", "processDis");
        return "process/flowmanage";
    }

    @RequestMapping("/processDis")
    public String sellectProcess(String baseKey, Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "8") int size, HttpSession session) {
        Integer userId = ((AoaUser) session.getAttribute("user")).getUserId();
        //总数据
        int totalElements = bursementService.selectAll(userId, baseKey, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaProcessList> list = bursementService.selectAll(userId, baseKey, (page - 1) * size, size);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);

        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("prolist", list);
        model.addAttribute("url", "processDis");
        return "process/managetable";
    }


}
